static float3 lytDir = {0,-1,-1};
static float4 lytClr = {1,1,1,1};
static float4 ambClr = {0.5f,0.525f,0.55f,1};
static float4 difClr = {1,1,1,1};

Texture2D    tex0;
SamplerState smp0;

cbuffer CBuffer
{
	float4 color;
	float4 eye;

	float4x4 world;
    float4x4 view;
	float4x4 pers;
	float4x4 orth;
}

struct Package
{
    float4 pos : SV_POSITION;
	float4 clr : COLOR0;
	float2 tex : TEXCOORD0;
};

struct Material
{
	float Ka, Kd, Ks, A;
};

float4 calcPhongLighting(Material M, float4 LColor,
	float3 N, float3 L, float3 V, float3 R )
{
	float4 Ia = M.Ka * ambClr;
	float4 Id = M.Kd * saturate(dot(N,L));
	float4 Is = M.Ks * pow(saturate(dot(R,V)),M.A);
	return Ia+(Id+Is)*LColor;
}

Package mainV(
	float4 pos : POSITION0,
	float3 nrm : NORMAL0,
	float2 tex : TEXCOORD0)
{
    Package output;

	output.pos = mul(pers,mul(view,mul(world,pos)));
	output.tex = tex;

	float3 d = normalize(lytDir);
	float3 p = mul((float3x3)world,pos);
	float3 n = normalize(mul((float3x3)world,nrm));
	float3 v = normalize(eye-p);
	float3 r = reflect(d,n);

	Material mat = {0.8f,1,0,0};
	output.clr = color*calcPhongLighting(
		mat,lytClr,n,-d,v,r);
	output.clr.a = 1;

    return output;
}

float4 mainP(Package input) : SV_TARGET
{
	return saturate(input.clr+tex0.Sample(smp0,input.tex));
}